// components/UploadImage/UploadImage.js
import { uploadFile } from '../../service/common-service.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    label: String,
    value: {
      type: String,
      observer: function (newData, oldData) {
        this.setData({ imageUrl: newData })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imageUrl: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    upload:function(){
      const _ = this;
      wx.chooseImage({
        count: 1,
        success: function (res) {
          console.log(res)
          if (res.errMsg === "chooseImage:ok" && res.tempFilePaths.length > 0) {
            uploadFile(res.tempFilePaths[0]).then(function (result) {
              console.log(result);
              const { status, message, data } = result;

              if (status === 0) {
                _.setData({ imageUrl: data.fileUrl });
                _.triggerEvent('change', {
                  value: data.fileUrl
                })
              }
            });
          }
        }
      })
    }
  }
})
